$(function () {
    // 初始化商品类型
    $.ajax({
        type: 'post',
        dataType: 'json',
        contentType: "application/json",
        url: '/type/getAllType',
        cache: false,
        async: true,
        success: function (data) {
            $.each(data,function (key,value) {
                $(".left_menu_ul").append('<li><a href="#" class="left_menu_a" value="'+ value.typeName  +'">'+value.typeName+'</a></li>');
            });

            var type = $(".left_menu_a").attr("value")== undefined ? '': $(".left_menu_a").attr("value").trim();; // 类型默认第一个元素
            type="";
            var sortWay = $(".sortItem option:selected").text()== undefined ? '': $(".sortItem option:selected").text().trim();;	// 排序方式
            var showNumber = $(".showNumber option:selected").text()== undefined ? '': $(".showNumber option:selected").text().trim();;	// 显示条数,默认显示6条
            var pagination = $(".pagination_a").html()== undefined ? '': $(".pagination_a").html().trim();; // 第几页，默认第一页

            // 类型点击
            $(".left_menu_a").each(function () {
                $(this).click(function () {
                    //  当前点击的类型id
                    type = $(this).attr("value");
                    requestGoodsByCondition();
                })
            });

            // 排序方式改变
            $(".sortItem").change(function () {
                sortWay = $(".sortItem option:selected").text();
                requestGoodsByCondition();
            });

            // 显示条数改变
            $(".showNumber").change(function () {
                showNumber = $(".showNumber option:selected").text();
                requestGoodsByCondition();
            });


            // 通过条件获取商品信息
            function requestGoodsByCondition() {
                $("#nGoodsList .product_li").remove();//清除数据
                $("#pagination ul").remove();//清除分页
                //封装方法
                var r ;//数据
                var numbers;//分页数据
                var conditionData = {
                    "typeName": type,
                    "sortWay": sortWay,
                    "pageNumber": showNumber,    // 分几页
                    "startPage": pagination      // 从第几页开始
                };
                var conditionDataJSON = JSON.stringify(conditionData);
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: '/goods/getGoodsByConditionAndPage',
                    data: conditionDataJSON,
                    contentType: "application/json",
                    cache: false,
                    async: true,
                    success: function (data) {

                        /**
                         *
                         * 所有分页带条件的数据在data里面
                         */
                        //清空原来,保留头部信息
                        if (data.list!=null || data.list!=""){
                            r=data.list;
                            for (var i = 0; i < r.length; i++) {
                                console.log(r[i].gname)
                                $("#nGoodsList").append('<div class="product_li"><div class="grid_3"><img class="sale" src="/images/new.png" alt="New"/><div class="prev"><a href="product_page.html"><img src="'+
                                    r[i].gimage+'" alt="" title="" /></a></div></div><div class="grid_4"><div class="entry_content"><a href="product_page.html"><h3 class="title">'+
                                    r[i].gname+'</h3></a><div class="review"><a class="plus" href="#"></a><a class="plus" href="#"></a><a class="plus" href="#"></a><a href="#"></a><a href="#"></a><span>1条评论)</span></div><p">'+
                                    r[i].introduction+'</p><a class="more" href="javaScript:void(0);">更多</a></div></div><div class="grid_2"><div class="cart"><div class="price"><div class="price_new">￥'+
                                    r[i].price+'</div><div class="price_old">￥'+
                                    r[i].price+'</div></div><a href="javascript:void(0);" class="bay" alt="'+
                                    r[i].gid+'">加入购物车</a><a href="javascript:void(0);" class="obn"></a><a href="javascript:void(0);" class="like"'+
                                    r[i].gid+'></a></div></div><div class="clear"></div></div>');
                            }

                            /*
                            分页数据绑定
                             */
                            if(data.navigatepageNums!=null || data.navigatepageNums!="") {
                                var i=0;
                                numbers =data.navigatepageNums;
                                var html ='<ul><li class="prev prefix_1next" value="'+data.prePage+'"><span>&#8592;</span></li>';
                                if(data.pageNum>1){
                                    i=data.pageNum-1;
                                }
                                for (i ; i < numbers.length; i++) {
                                    if (numbers[i]==data.pageNum) {
                                        html+='<li class="curent" ><a href="javaScript:void (0);" class="pagination_a">'+numbers[i]+'</a></li>';
                                    }
                                    if (numbers[i]>=(data.pageNum+3)) {
                                        html+=' <li><span disabled ="disabled">...</span></li>';
                                        html+='<li><a href="javaScript:void (0);" class="pagination_a">'+numbers[i]+'</a></li>';
                                        break;
                                    }else {
                                        if (numbers[i]!=data.pageNum) {
                                            html+='<li ><a href="javaScript:void (0);"  class="pagination_a">'+numbers[i]+'</a></li>';
                                        }
                                    }
                                }
                                html+='<li class="next prefix_1next" value="'+data.nextPage+'"><a href="javaScript:void (0);">&#8594;</a></li></ul>';

                                $("#pagination").append(html);

                                // 第几页改变
                                $(".pagination_a").each(function () {
                                    $(this).click(function () {
                                        pagination = $(this).html();
                                        $(this).parent().addClass('curent');
                                        requestGoodsByCondition();
                                    })
                                });
                                $(".prefix_1next").click(function () {
                                    pagination = $(this).attr("value");
                                    if (pagination==0) {
                                        alert("这是第一页啦！");
                                    }else  if(pagination==numbers[numbers.length]){
                                        alert("这是最后一页啦！");
                                    } else {
                                        requestGoodsByCondition();
                                    }

                                })

                            }

                        }else{

                        }
                        console.log(data);
                    }
                });
            }
            requestGoodsByCondition();
        }
    });
});